package org.websocket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.websocket.dto.WebSocketBuffer;
import org.websocket.service.WebSocketServer;

@RequestMapping("/websocket/")
@RestController
public class WebSocketController {

    private final WebSocketServer webSocketServer;

    @Autowired
    public WebSocketController(WebSocketServer webSocketServer) {
        this.webSocketServer = webSocketServer;
    }

    @PostMapping("/sendMessage")
    public String sendMessage(@RequestBody WebSocketBuffer webSocketBuffer) {
        webSocketServer.pushRealTimeMessages(webSocketBuffer);
        return webSocketBuffer.toString();
    }
}
